public class MySingleList implements ILinkedList{
    //定义静态内部类 来表示 节点对象
    static class ListNode{
        public int val; //数据
        public ListNode next; //节点的引用

        public ListNode(int val){
            this.val = val;
        }
    }

    //存储链表的头节点的引用
    public ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        //0.如果链表当中一个元素都没有 此时 插入的节点 就是第一个节点
        if(head == null){
            head = node;
            return;
        }
        //1.找到链表的尾巴
        ListNode cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        //此时cur指向的节点 就是尾节点
        cur.next = node;
    }

    private void checkPos(int index){
        if(index < 0 || index > size()){
            throw new CheckPosException("index位置不合法：" + index);
        }
    }

    private ListNode findIndex(int index){
        ListNode cur = head;
        int count = 0;
        while(count != index - 1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public void addIndex(int index, int data) {
        checkPos(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        //中间位置的插入
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null){
            System.out.println("链表为空，无法删除");
            return;
        }

        //1. 要删除的节点 是头节点
        if(head.val == key){
            head = head.next;
            return;
        }
        //2.删除的节点是其他节点
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                return;
            }else{
                cur = cur.next;
                prev = prev.next;
            }
        }

        System.out.println("没有你要删除的数字：" + key);
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null){
            System.out.println("链表为空，无法删除");
            return;
        }

        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                cur = cur.next;
                prev = prev.next;
            }
        }
        //最后判断一次头节点
        if(head.val == key){
            head = head.next;
        }
    }

    @Override
    public int size() {
        int usedSize = 0;
        ListNode cur = head;
        while(cur != null){
            usedSize++;
            cur = cur.next;
        }
        return usedSize;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        /*ListNode cur = head;
        while(cur != null){
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }*/
        head = null;
    }

}
